<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Ringo Chan <334258471@qq.com> <http://www.jxlnxx.com/>
// +----------------------------------------------------------------------

namespace Api\Controller;
use Com\Message;
/**
 * API公共控制器
 * 主要获取公共数据
 */
class PublicController extends ApiController {
    
    /**
     * 获取验证码
     *
     * @author Ringo Chan<334258471@qq.com>
     */
    public function verify(){
        params_exist(array('user', 'type'));
        $username = I('post.user');
        $type = I('post.type');
        empty($username) && response(-1, '', '手机号码不能为空');
        empty($type) && response(-1, '', '类型不能为空');
        !preg_match('/1[34578]{1}\d{9}$/', $username) && response(-2, '', '请填入正确的手机号码');
        !in_array($type, array('register', 'forgotten', 'modifymobile')) && response(-2, '', '类型不正确');
        
        if ($type == 'register') {
            $Ucenter_member = M('Ucenter_member');
            $info = $Ucenter_member->where(array('username'=>$username))->find();
            $info && response(-3, '', '账户已存在');
        }
         
        $code = getRandCode(6);
         
        $data = array(
            'username' => $username,
            'type' => $type,
            'code' => $code,
            'create_time' => NOW_TIME,
            'deadline' => NOW_TIME + 1800,
            'status' => 1
        );
         
        $Message_verify = M('Message_verify');
         
        $Message_verify->create($data);
        $Message_verify->add();
        Message::send($username,'#code#='.$code,'49573');
        response(1, array('verify'=>$code));
    }



    /**
     * 获取省份列表
     *
     * @author Ringo Chan<334258471@qq.com>
     */
    public function getProvinces(){
         $list = getProvinces();

         $list ? response(1, $list) : response(-1, '', '没有找到匹配对象');
    }
    
    /**
     * 获取对应省份的城市列表
     * 
     * @param string province_id 省份标识
     * @author Ringo Chan<334258471@qq.com>
     */
    public function getCities(){
        params_exist(array('province_id'));
        
        $province_id = I('post.province_id');
        
        $list = getCities($province_id);
         
        $list ? response(1, $list) : response(-1, '', '没有找到匹配对象');
    }
    
    /**
     * 获取对应城市的区县列表
     * 
     * @param string city_id 城市标识
     * @author Ringo Chan<334258471@qq.com>
     */
    public function getAreas(){
        params_exist(array('city_id'));
        
        $city_id = I('post.city_id');
        
        $list = getAreas($city_id);
         
        $list ? response(1, $list) : response(-1, '', '没有找到匹配对象');
    }

    /**
     * app版本
     * @param string os
     */
    public function appVersion(){
        $M_app_version = M('app_version');
        
        $map=array();
        $os=I('os');
        if($os){
            $map['os']=$os;
        }
        
        $list=$M_app_version->where($map)->select();
        $list ? response(1,$list) : response(-2);
    }
    
    /**
     * app版本
     * @param int $id
     */
    public function setup(){
       $info = M('setup')->find();
       $info['logo'] = M('picture')->where('id='.$info['logo'])->getField('path');
       
        $info ? response(1,$info) : response(-2);
    }

    /**
     * 支付配置
     */
    public function services(){
        $result=M('payment_setting')->find();
        $result['qq'] = M('setup')->getField('qq');
        response(1,$result);
    }

    /**
     * 代理列表
     */
    public function proxy(){
        $result = M('proxy_recharge')->select();
        foreach ($result as &$res){
            $img=M('picture')->where('id='.$res['proxy_id'])->getField('path');
            $res['img_url'] =C('BASE_URI').$img;
        }
        response(1,$result);
    }
}
?>